//
//  PeriodicalViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/18.
//  Copyright © 2018年 cxd. All rights reserved.
//  期刊

#import "PeriodicalViewController.h"
#import "PeriodicalSearchView.h"
#import "BookCollectCell.h"
#import "PeriodicalHeaderCollectionReusableView.h"
#import "PeriodicalDetailViewController.h"
#import <WebKit/WebKit.h>

@interface PeriodicalViewController () <UICollectionViewDelegate,UICollectionViewDataSource,WKNavigationDelegate>

@property (nonatomic, strong)PeriodicalSearchView *searchView;
@property (nonatomic, strong)UICollectionView *listCollectionView;

@property (nonatomic, strong)WKWebView *webView;
@property (nonatomic, strong)UIButton * btn;
@property (nonatomic, strong) UIView *topview;

@end

@implementation PeriodicalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self buildUI];
}

- (void)buildUI {
    
    [self.view addSubview:self.webView];
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(5, 5, 80, 40);
    _btn.backgroundColor = [UIColor clearColor];
    [_btn setTintColor:[UIColor whiteColor]];
    _btn.layer.cornerRadius = 5;
    _btn.layer.masksToBounds = YES;
    _btn.hidden = YES;
    [_btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [_btn setTitle:@"" forState:UIControlStateNormal];
    [self.view addSubview:_btn];
    

}
- (void)clickBack{
    if ([_webView canGoBack]) {
        [_webView goBack];
    }
}
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//    if (navigationAction.targetFrame == nil) {
//        [webView loadRequest:navigationAction.request];
//    }
    NSString *url1=navigationAction.request.URL.absoluteString;
   
    if ([url1 rangeOfString:@"?"].location != NSNotFound) {
        NSRange range = [url1 rangeOfString:@"?"];//匹配得到的下标
        NSString *string = [url1 substringToIndex:range.location];//截取范围类的字符串
        url1 = string;
    }
    

    //2.做你想做的操作
    if ([url1 isEqualToString:@"http://vertical.183read.cc/wqjd.html"] || [url1 isEqualToString:@"http://vertical.183read.cc/dynamic_wqjd.html"]) {
        self.btn.hidden = YES;
        self.topview.backgroundColor = [UIColor whiteColor];
        self.webView.frame = CGRectMake(0, -145, kScreenWidth, kScreenHeight - CXDTopHeight - 70 - 49 + 145);
    } else {
      
        self.btn.hidden = NO;
        self.topview.backgroundColor = [UIColor clearColor];
        self.webView.frame =CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight - 70 - 49);


    }
 

    decisionHandler(YES);
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {

    NSString*injectionJSString =@"var script = document.createElement('meta');"

    "script.name = 'viewport';"

    "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=3.0, minimum-scale=1.0, user-scalable=yes\";"

    "document.getElementsByTagName('head')[0].appendChild(script);";

    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://vertical.183read.cc/wqjd.html"]]];////PeriodicalURLhttp://vertical.183read.cc/journal/read_wqjd_667775.html
}
//-(void)reloadUrl{
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:PeriodicalURL]]];
//}
#pragma mark - 懒加载
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, -145, kScreenWidth, kScreenHeight - CXDTopHeight - 70 - 49 + 145)];
        _webView.navigationDelegate = self;
        self.topview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _webView.frame.size.width, 145)];
        self.topview.backgroundColor = [UIColor whiteColor];
        [_webView addSubview:self.topview];
//        _webView.allowsBackForwardNavigationGestures = YES;
    }
    return _webView;
}
- (PeriodicalSearchView *)searchView {
    if (!_searchView) {
        _searchView = [[PeriodicalSearchView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 60)];
    }
    return _searchView;
}

- (UICollectionView *)listCollectionView {
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth ) / 3, 180);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.headerReferenceSize = CGSizeMake(kScreenWidth, 65);
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.searchView.bottom, kScreenWidth, 990) collectionViewLayout:layout];
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
        _listCollectionView.backgroundColor = RGB(238, 238, 238);
        _listCollectionView.showsVerticalScrollIndicator = NO;
        _listCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
        [_listCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
        [_listCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PeriodicalHeaderCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PeriodicalHeaderCollectionReusableView class])];
    }
    return _listCollectionView;
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PeriodicalDetailViewController *detailVC = [[PeriodicalDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        PeriodicalHeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([PeriodicalHeaderCollectionReusableView class]) forIndexPath:indexPath];
        if (indexPath.section == 0) {
            header.nameLabel.text = @"新闻人物";
        }else if (indexPath.section == 1) {
            header.nameLabel.text = @"休闲娱乐";
        }else if (indexPath.section == 2) {
            header.nameLabel.text = @"商业财经";
        }else if (indexPath.section == 3) {
            header.nameLabel.text = @"汽车数码";
        }
        return header;
    }
    return nil;
}

@end
